
function basePath(){
    //获取当前网址，如： http://localhost:8080/ems/Pages/Basic/Person.jsp
    var curWwwPath = window.document.location.href;
    //获取主机地址之后的目录，如： /ems/Pages/Basic/Person.jsp
    var pathName = window.document.location.pathname;
    var pos = curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8080
    var localhostPath = curWwwPath.substring(0, pos);
    //获取带"/"的项目名，如：/ems
    var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
    //获取项目的basePath   http://localhost:8080/ems/
    var basePath=localhostPath+projectName+"/";
    return basePath;
};

jQuery(document).ready(function() {
	var orgData = {};
	$.ajax({
		async : false,
		url : projectPath + "user/getCurrentUser.do",
		data : {},
		dataType : "json",
		success : function(data) {
			orgData = data;
			$('#orgSelect').val(orgData.orgname);
			$('#orgSelect').attr('data', orgData.orgcode);
		}
	});
	bindDict("consulttype","COMPLAIN_TYPE","");
	bindDict("consultstatus","COMPLAIN_STATUS","");
	bindDict("businesstype","DM_XKXMLX","");
//	bindOrg1('orgSelect');
	bindOrg2('orgSelect', '', '');
	initTable();
	
	
	App.setPage("consulting");  //Set current page
	App.init(); //Initialise plugins and elements

	//列表查询事件
	$('#search').click(function(){
		$('#consultTable').bootstrapTable(('refresh')); 
	});
	
	//刷新数据事件
	$('#refresh').click(function(){
		$('#consultTable').bootstrapTable(('refresh'));
	});
	
	//保存数据事件
	$('#save').click(function(){

		$("#ifModal")[0].contentWindow.save();
		
	});
	
	$('#starttime').change(function(){
		if('' != $('#endtime').val()){
			if(Date.parse($('#starttime').val()) > Date.parse($('#endtime').val())){
				$('#endtime').val('');
			}
		}
		if('' == $('#starttime').val()){
			$('#endtime').removeAttr('min');
		} else {
			$('#endtime').attr('min', $('#starttime').val());
		}
	});
	
});	
//js函数
function initTable(){	
	
	$('#consultTable').bootstrapTable({
		method: 'post',
	    url: basePath() + 'consulting/list.do',
	    pagination: true, //分页
	    dataType: "json",
        contentType: "application/json",
        pageList: [10, 20, 50, 100],  //可供选择的每页的行数（*）
        striped: true,      //是否显示行间隔色
        sidePagination: "server",
        ////查询参数,每次调用是会带上这个参数，可自定义                         
        queryParams:function(params) {
            return {
                	pageNum: params.offset / params.limit + 1,
                	pageSize: params.limit,
                	complainTitle: $('#consulttitle').val(),
                	complainType: $('#consulttype').val(),
                	complainStatus: $('#consultstatus').val(),
                	startTime: $('[name="starttime"]').val(),
                	endTime: $('[name="endtime"]').val(),
                	businessType: $('#businesstype').val(),
                	orgCode: $('#orgSelect').attr("data")
                };
        },
	    columns: [{  
            field : 'number',  
            title : '序号',  
            align : 'center',
            width : '40px',
            formatter : function(value, row, index) { 
// 		                return index + 1;  
                var page = $('#consultTable').bootstrapTable("getPage");  
                return page.pageSize * (page.pageNumber - 1) + index + 1;  
            }  
        }, {
	        field: 'complainId',
	        visible: false
	    }, {
	        field: 'complainTitle',
	        title: '标题',
	    }, {
	        field: 'complainType',
	        title: '咨询投诉类型'
	    }, {
	        field: 'businessType',
	        visible: false
	    }, {
	    	field: 'businessTypeName',
	    	title: '业务类型'
	    }, {
	        field: 'orgName',
	        title: '咨询投诉机构'
	    }, {
	        field: 'orgCode',
	        visible: false
	    }, {
	        field: 'complainStatus',
	        title: '状态',
	        formatter:function(value,row,index){
	        	var result = value;
	        	if(value == "1")
	        		result = '<span>已回复</span>';
	        	else {
	        		result = '<span style="color:#ed5565" >未处理</span>';
	        	}
	        	return result;
	        }
	    }, {
	        field: 'leaveTime',
	        title: '留言时间'
	    }, {
            field : 'setQuestions',
            title: '设为常见问题',
            width : '100px',
            events : operateEvents,
            formatter: function(value, row, index){
            	var result = '-';
            	if(row.complainStatus == '1' && row.complainType == '问题咨询' && value == '1'){
            		result = '<input class="questions" type="checkbox" checked="checked">';
            	} else if(row.complainStatus == '1' && row.complainType == '问题咨询') {
            		result = '<input class="questions" type="checkbox">';
            	}
            	return result;
            }
        }, {
			field : 'operate',
			title : '操作',
			align : 'center',
			width : '120px',
			events : operateEvents,
			formatter : operateFormatter
		}]
	});
}

function operateFormatter(value, row, index) {
	if(row.complainStatus == '1'){
		return [
		        '<button type="button" class="edit btn btn-info  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-edit"></span>详情</button>',
		        ].join('');
	} else {
		return [
		        '<button type="button" class="edit btn btn-danger  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-edit"></span>回复</button>',
		        ].join('');
	}
}

window.operateEvents = {
	'click .edit': function (e, value, row, index) {
		$("#save").css("display","inline");
		if(row.complainStatus == '1'){
			$("#save").css("display","none");
		}
    	var complainId = row.complainId;
    	
    	setModalSize();
    	
    	$('#ifModal').attr("src", "edit.html?complainId=" + complainId);
    	
		$('#modalTile').html("咨询投诉");
		$("#modal").modal('show');
    },
    'click .questions': function (e, value, row, index) {
    	if(value == '1'){ // 取消为常见问题
    		$.ajax({
				url : projectPath + 'questions/deleteByMsgId.do',
				method: 'post',
				data : {
					"msgId" : row.complainId
				},
				dataType : "json",
				success : function(data) {
					if (data.result == 1) {
	    				$('#consultTable').bootstrapTable(('refresh'));
					}
				}
			});
    	} else { // 设置为常见问题
    		var now = new Date();
    		var dayStr = now.getDate() > 9 ? now.getDate() : '0' + now.getDate();
    		var monStr = (now.getMonth() + 1) > 9 ? (now.getMonth() + 1) : '0' + (now.getMonth() + 1);
    		var dateStr = now.getFullYear() + '-' + monStr + '-' + dayStr;
    		var questionsData = {};
    		questionsData.msgId = row.complainId;
    		questionsData.title = row.complainTitle;
    		questionsData.businessType = row.businessType;
    		questionsData.orgCode = row.orgCode;
    		questionsData.content = row.leaveMessage;
    		questionsData.replyContent = row.replyMessage;
    		questionsData.status = '1';
    		questionsData.publishDate = dateStr;
    		$.ajax({
    			method: 'post',
    			url: basePath() + 'questions/save.do',
    			data: JSON.stringify(questionsData),
    			dataType: "json",
    	        contentType: "application/json",
    			success: function(data){
    				$('#consultTable').bootstrapTable(('refresh'));
    	      	}
    		});
    	}
    }
};


